//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "ListSMIData.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <fstream>

#include <zlib.h>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHTEXT 100
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ListSMIData::ListSMIData () {
  
  this->filenameIn  = new String () ;
  this->filenameOut = new String ();

  this->smilesData  = new TListE <SMIData> ();
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ListSMIData::ListSMIData (const ListSMIData& listSMIData) {
  
  this->filenameIn  = new String (listSMIData.filenameIn) ;
  this->filenameOut = new String (listSMIData.filenameOut);

  this->smilesData  = new TListE <SMIData> (listSMIData.smilesData);
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ListSMIData::ListSMIData (const ListSMIData* listSMIData) {
  
  this->filenameIn  = new String (listSMIData->filenameIn) ;
  this->filenameOut = new String (listSMIData->filenameOut);

  this->smilesData  = new TListE <SMIData> (listSMIData->smilesData);

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ListSMIData::~ListSMIData () {

  if (this->filenameIn)  delete this->filenameIn;
  if (this->filenameOut) delete this->filenameOut;

  if (this->smilesData)  delete this->smilesData;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void ListSMIData::FilenameIn (String* filenameIn) {
  
  if (filenameIn) this->filenameIn->In(filenameIn);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void ListSMIData::FilenameOut (String* filenameOut) {
  
  if (filenameOut) this->filenameOut->In(filenameOut);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void ListSMIData::SmilesData (TListE <SMIData>* smilesData) {
  
  if (smilesData) *(this->smilesData) = *(smilesData);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* ListSMIData::FilenameIn (void) {
  
  return this->filenameIn;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* ListSMIData::FilenameOut (void) {
  
  return this->filenameOut;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
TListE <Ligand>* ListSMIData::SmilesData (void) {
  
  return this->smilesData;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool ListSMIData::LoadData (void) {
  
  if (!this->filenameIn || !this->filenameIn->Length()) return false;

  bool isGZFile = (this->filenameIn->Contain("gz"))?1:0;

  fstream file;
  gzFile  zipFile;
  
  char    buffer [LENGTHTEXT + 1];
  
  if (isGZFile) { 
    zipFile = gzopen (this->filenameIn->Out(), "rb9"); 
    if (!zipFile) return false;
  }
  else {
    file.open (this->filenameIn->Out(), fstream::in);
    if (!file.is_open()) return false;
  }

  while (!((isGZFile)?gzeof(zipFile):file.eof())) {
    
    if (isGZFile) gzgets(zipFile, buffer, LENGTHTEXT);
    else          file.getline(buffer, LENGTHTEXT);
    
    if (!strlen(buffer)) continue;

    SMIData* newSMIData = new SMIData ();
    
    if (newSMIData->LoadData(buffer)) this->smilesData->Add(newSMIData);
    else                              delete newSMIData;

  }
  
  if (isGZFile) gzclose(zipFile);
  else          file.close();

  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
ListSMIData ListSMIData::operator= (const ListSMIData& listSMIData) {
  
  this->filenameIn->In(listSMIData.filenameIn) ;
  this->filenameOut->In(listSMIData.filenameOut);

  *(this->smilesData) = *(listSMIData.smilesData);
  
  return *this;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
